package com.bj.dljd;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.bj.dljd.mapper.DicPayTypeMapper;
import com.bj.dljd.mapper.DicPayWayMapper;
import com.bj.dljd.models.dic.DicItem;
import com.bj.dljd.models.mapping.PayWayGroup;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@MapperScan("com.bj.dljd.mapper")
@SpringBootApplication
public class PowerBoosApplication  implements CommandLineRunner {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor plusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }



    public static void main(String[] args) {
        SpringApplication.run(PowerBoosApplication.class, args);
    }

    @PostConstruct
    public void onApplicationEvent() {
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        System.out.println("开启项目只运行换一次");
    }
    // 静态的 支付方式名称
    public static Map<String,String> NAME_PAY_WAY = new HashMap<>();
    public static Map<String,String> NAME_PAY_TYPE = new HashMap<>();

    // 静态的 支付方式字典列表
    public static List<DicItem> PAY_WAY_DIC_LIST = new ArrayList<>();
    // 静态的 支付方式字典列表
    public static Map<String,List<DicItem>> PAY_TYPE_DIC_MAP = new HashMap<>();

    @Resource
    private ApplicationContext applicationContext;


    /**
     *   run() : 在spring 容器初始化完成后立即执行的
     *            可以做项目初始化操作 比如读取数据库的常用数据 读取文件（properItem）
     *            设置参数 变量等等
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        // 读取dic_pay_way 表数据 初始化到内存中
        // 从spring 容器中对象中获取dic_oay_way 表对应的Mapper对象
        DicPayWayMapper dicPayWayMapper
                = applicationContext.getBean(DicPayWayMapper.class);
        List<DicItem> dicItems = dicPayWayMapper.selectPayWayDicList();
        PAY_WAY_DIC_LIST = dicItems;
        //存储支付方式的名称
        PAY_WAY_DIC_LIST.stream().forEach(dicItem -> {
            NAME_PAY_WAY.put(dicItem.getCode(),dicItem.getLabel());
        });

        // 支付类型字典数据
        DicPayTypeMapper dicPayTypeMapper
                = applicationContext.getBean(DicPayTypeMapper.class);
        // list的每一个成员都是一个分组数据
        List<PayWayGroup> payTypeGroupWayCode =
                dicPayTypeMapper.selectPayTypeGroupWayCode();


        for (PayWayGroup payWayGroup : payTypeGroupWayCode) {
            String payWayCode = payWayGroup.getPayWayCode(); // 支付方式的编码
            String groupData = payWayGroup.getGroupData(); // 分组数据
            List<DicItem> payTapeDicList = new ArrayList<>();
            // 将分组数据 转换成 DicItem 对象
            for (String data : groupData.split(";")) {
                String[] split = data.split("#");
                String code = split[0];// code
                String label = split[1];// code

                DicItem dicItem = new DicItem(code,label);
                payTapeDicList.add(dicItem);
                NAME_PAY_TYPE.put(payWayCode+":"+code,label);
            }
             PAY_TYPE_DIC_MAP.put(payWayCode,payTapeDicList);
        }
    }
}
